//
//  ViewPagerIndicator.swift
//  navigation
//
//  Created by tuwan on 2018/1/30.
//  Copyright © 2018年 czmc .Inc. All rights reserved.
//

import UIKit
protocol ViewPagerIndicatorDelegate : class{
    func onTitleClick(currentIndex:Int)
    func getViewPager()->ViewPager
}

private let lineHeight:CGFloat = 3;
class ViewPagerIndicator : UIView{
    private var mTitles:[String];
    private var itemWidth:CGFloat = 0;
    private let itemHeight:CGFloat = 50;
    private var currentIndex:Int = 0;
    private var lastIndex:Int = 0;
    private weak var indicatorDelegate:ViewPagerIndicatorDelegate?
    init(frame:CGRect,titles:[String]) {
        self.mTitles = titles
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        meature();
        createTitleLabel();
        createLine();
        setCurrentIndex(index: 0);
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension ViewPagerIndicator{

    func meature(){
        guard let titles:[String] = mTitles else {
           return
        }
       itemWidth = CGFloat( Int(frame.width) / titles.count)
    }

    func createTitleLabel(){
        for var index in 0...mTitles.count-1{
            let b = UIButton.init(type: UIButtonType.custom)
            b.setTitle( mTitles[index],for: UIControlState.normal)
            b.frame = CGRect(x:CGFloat(index) * itemWidth,y:0,width:itemWidth,height:itemHeight-lineHeight)
            b.setTitleColor(UIColor.gray, for: UIControlState.normal)
//            b.setTitleColor(UIColor.black, for: UIControlState.highlighted)
            b.tag = index
            b.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside)
            addSubview(b)
        }
    }
    func createLine(){
        let lineView = UIView.init(frame: CGRect(x:0,y:itemHeight-lineHeight,width:itemWidth,height:lineHeight))
        lineView.backgroundColor = UIColor.randomColor
        addSubview(lineView)
    }
    
    @objc func click(v:UIView){
        setCurrentIndex(index: v.tag)
        if(indicatorDelegate != nil){
            indicatorDelegate?.onTitleClick(currentIndex: currentIndex)
        }
    }


}

//对外暴露的接口
extension ViewPagerIndicator{
    func  setCurrentIndex(index:Int){
        self.lastIndex =  self.currentIndex
        self.currentIndex = index
        
        let lastView = subviews[lastIndex] as! UIButton
        lastView.setTitleColor(UIColor.gray, for: UIControlState.normal)
        
        let currentView = subviews[currentIndex] as! UIButton
        currentView.setTitleColor(UIColor.black, for: UIControlState.normal)
    }
    func setViewPagerIndicatorDelegate(delegate:ViewPagerIndicatorDelegate) {
        self.indicatorDelegate = delegate
        self.indicatorDelegate?.getViewPager().setViewPagerDelegate(delegate: self)
    }
    
}
extension ViewPagerIndicator : ViewPagerDelegate{
    func onViewPagerChange(currentIndex: Int,  duration: CGFloat) {
        setCurrentIndex(index: currentIndex)
        subviews[subviews.count-1].frame = CGRect(x:duration*frame.width,y:itemHeight-lineHeight,width:itemWidth,height:lineHeight)
    }
}
